package cn.chengpengper.seckill.goods.interfaces.controller;

import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.model.dto.SeckillGoodsDTO;
import cn.chengpengper.seckill.common.response.ResponseMessage;
import cn.chengpengper.seckill.common.response.ResponseMessageBuilder;
import cn.chengpengper.seckill.goods.application.command.SeckillGoodsCommand;
import cn.chengpengper.seckill.goods.application.service.SeckillGoodsService;
import cn.chengpengper.seckill.goods.model.entity.SeckillGoods;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chengpeng.hu
 */
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*", originPatterns = "*")
public class SeckillGoodsController {

    private final SeckillGoodsService seckillGoodsService;

    /**
     * 保存秒杀商品
     *
     * @param seckillGoodsCommand 商品实体
     */
    @PostMapping("/v1/saveSeckillGoods")
    public ResponseMessage<String> saveSeckillGoods(@RequestBody SeckillGoodsCommand seckillGoodsCommand) {
        seckillGoodsService.saveSeckillGoods(seckillGoodsCommand);
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode());
    }

    /**
     * 根据商品ID获取商品详情
     *
     * @param id 商品ID
     * @return 商品详情
     */
    @GetMapping("/v1/getSeckillGoodsById")
    public ResponseMessage<SeckillGoods> getSeckillGoodsById(@RequestParam Long id) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillGoodsService.getSeckillGoodsId(id));
    }

    /**
     * 根据描述活动ID获取所含商品列表
     *
     * @param activityId 活动ID
     * @return 商品列表
     */
    @GetMapping("/v1/getSeckillGoodsByActivityId")
    public ResponseMessage<List<SeckillGoods>> getSeckillGoodsByActivityId(@RequestParam Long activityId) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillGoodsService.getSeckillGoodsByActivityId(activityId));
    }

    /**
     * 更新商品状态
     *
     * @param status 商品状态
     * @param id     商品ID
     */
    @PutMapping("/v1/updateStatus")
    public ResponseMessage<String> updateStatus(@RequestParam Integer status, @RequestParam Long id) {
        seckillGoodsService.updateStatus(status, id);
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode());
    }

    /**
     * 获取商品列表(带缓存)
     *
     * @param activityId 活动ID
     * @param version    版本
     * @return 商品列表
     */
    @GetMapping("/v1/getSeckillGoodsList")
    public ResponseMessage<List<SeckillGoodsDTO>> getSeckillGoodsByActivityId(@RequestParam Long activityId,
                                                                              @RequestParam Long version) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillGoodsService.getSeckillGoodsList(activityId, version));
    }

    /**
     * 获取商品详情（带缓存）
     *
     * @param id      商品ID
     * @param version 版本
     * @return 商品详情
     */
    @GetMapping("/v1/getSeckillGoods")
    public ResponseMessage<SeckillGoodsDTO> getSeckillGoods(@RequestParam Long id, @RequestParam Long version) {
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillGoodsService.getSeckillGoods(id, version));
    }

}
